package cn.faifai.web.api;

import cn.faifai.common.page.TableDataInfo;
import cn.faifai.common.component.R;
import cn.faifai.tagapi.entity.IFile;
import cn.faifai.tagapi.dto.*;
import cn.faifai.tagapi.enums.fileSaveCode;
import cn.faifai.tagapi.service.FileUrlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

@RestController
@RequestMapping("/faifaiTag/fileUrl")
public class FileUrlController {

    @Autowired
    private FileUrlService fileUrlService;

    /**
     * 获取文件元信息
     */
    @GetMapping("/{id}")
    public R getFileUrlInfo(@PathVariable Long id){
        return R.ok(fileUrlService.getFileUrlInfo(id));
    }

    @PostMapping("/subPath")
    public R getSubPathm(@RequestBody FileUrlDto fileUrlDto){
        return R.ok(fileUrlService.getSubPath(fileUrlDto.getFileUrl()));
    }

    @GetMapping("")
    public R getSubPath(String path){
        System.out.println(path);
        List<String> subPath = fileUrlService.getSubPath(/*path.replaceFirst("/", "")*/path);
//        subPath.forEach(item->item.replace("\\","/"));
//        subPath.forEach(System.out::println);
        return R.ok(subPath);
    }

    @GetMapping("/subPath")
    public R getSubPath2(String path){
        SubDirectoryDto subPath = fileUrlService.scanDirectory(path);
        return R.ok(subPath);
    }

    @GetMapping("/registerFile")
    public R registerFile(String path) throws IOException, NoSuchAlgorithmException {
        fileUrlService.registerFile(path);
        return R.ok();
    }

    @GetMapping("/openFile")
    public R openFile(String path) throws IOException {
        fileUrlService.openFile(path);
        return R.ok();
    }

    /**
     * 标记单个文件
     */
    @PostMapping("/tagFile")
    public R tagFile(@RequestBody FileUrlDto fileUrlDto) throws IOException, NoSuchAlgorithmException {
        Response checkResponse = fileUrlService.checkFileRegister(fileUrlDto.getFileUrl());
        Integer flag = checkResponse.getCode();
        IFile iFile = (IFile) checkResponse.getData();
        if (flag == fileSaveCode.FILE_NOT_EXIST.getCode()){
            Response registResponse = fileUrlService.registerFile(fileUrlDto.getFileUrl());
            flag = registResponse.getCode();
            iFile = (IFile) registResponse.getData();
            fileUrlDto.setId(((IFile)registResponse.getData()).getId());
        }
        if (flag == fileSaveCode.FILE_REGISTERED.getCode()){
            fileUrlDto.setId(iFile.getId());
            flag = fileUrlService.tagFile(fileUrlDto);
        }
        return flag == fileSaveCode.SUCCESS.getCode() ? R.ok() : R.fail(fileSaveCode.getMessage(flag));
    }

    /**
     * 获取注册文件列表
     */
    @GetMapping("/getFileList")
    public TableDataInfo getFileList(FileUrlQuery<IFile> query, PageQuery pageQuery){
        return fileUrlService.getFileList(query, pageQuery);
    }

    /**
     * 根据tagid查询分页查询文件
     */
    @GetMapping("/getFileListByTagId/{tagId}")
    public TableDataInfo getFileListByTagId(@PathVariable Long tagId,@ModelAttribute PageQuery pageQuery){
        return fileUrlService.getFileListByTagId(tagId, pageQuery);
    }

    /**
     * 删除文件
     */
    @DeleteMapping("/{ids}")
    public R deleteFile(@PathVariable List<Long> ids){
        fileUrlService.deleteRegisterFile(ids);
        return R.ok();
    }
}
